define(["jquery", "easy-admin"], function ($, ea) {

    let init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'user.money_change/index',
        modify_url: 'user.money_change/modify',
        // recharge_url: 'user.recharge/inc',
        // decmoney_url: 'user.recharge/dec',
    };
    //先获取change_type_list
    var change_type_list = {};
    ea.request.post({url:'typeList',function(d){
            console.log(d)
            change_type_list = d;
        },function(){
            change_type_list = {};
        }
    });
    let Controller = {
        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh'],
                // sort: ['id','create_time'],
                cols: [[
                    {type: "checkbox"},
                    {field: 'id', width: 80, title: 'ID',searchOp:'=',sort:true,},
                    // {field: 'order_sn', minWidth: 160, title: '订单号',searchOp:'='},
                    {field: 'user_id', minWidth: 80, title: '变更会员',searchOp:'=',templet:function (d){
                            return '<div style="text-align: center;">' +
                                '<span class="">ID：'+d.uid+'</span>' +
                                '<br/><span class="">昵称：'+d.user.nickname+'</span></div>';
                        }
                    },
                    {field: 'related_uid', minWidth: 80, title: '相关会员',searchOp:'=',templet:function (d){
                            if(d.fromuser){
                                return '<div style="text-align: center;">' +
                                    '<span class="">ID：'+d.related_uid+'</span>' +
                                    '<br/><span class="">昵称：'+d.fromuser.nickname+'</span></div>';
                            }else{
                                return '系统';
                            }

                        }
                    },
                    // {field: 'user.nickname', minWidth: 80,search: false, title: '会员昵称'},
                    {field: 'change_type', title: '变更类型', minWidth: 50,selectList: {//TODO 不支持变量，暂时只能写死
                        1: "陪护床租金", 21: "微信充值", 22: "微信退款", 31: "后台充值", 32: "后台扣除"
                    }},
                    {field: 'before_money', title: '变更前金额', minWidth: 50,searchOp: 'number_range'},
                    {field: 'money', title: '变更金额', minWidth: 50,templet:function (d){
                            if(d.inc_or_dec == 1){//增加
                                return '<span style="color: #ff0000">+'+d.money+'</span>'
                            }else{//减少
                                return '<span style="color: #0000ff">-'+d.money+'</span>'
                            }
                        },searchOp: 'number_range'
                    },
                    {field: 'create_time', minWidth: 50, title: '记录时间', search: 'range',templet:function (d){
                            // let update_at = d.update_time||'未支付';
                            return '<div style="text-align: left;"><span class="">生成：'+d.create_time+'</span>' +
                                '<br/><span class="">修改：'+d.update_time+'</span></div>';
                        }
                    },
                    {field: 'remark', minWidth: 80, title: '备注', edit: 'text'},
                    // {field: 'end_time', minWidth: 50, title: '订单结束时间', search: 'range'},
                    // {field: 'pay_time', minWidth: 50, title: '订单支付时间', search: 'range'},
                    // {
                    //     width: 100,
                    //     title: '操作',
                    //     templet: ea.table.tool,
                    //     operate: [
                    //
                    //     ]
                    // }
                ]],
            });

            ea.listen();
        },
        add: function () {
            ea.listen();
        },
        edit: function () {
            ea.listen();
        },
        inc: function () {
            layui.form.on('submit(' +'save_form_1'+')', function (data) {
                let option = {url:'inc',data:data.field};
                ea.request.ajax('post',option,
                function(res){
                        ea.msg.success(res.msg);
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        setTimeout(function (){
                            parent.$(".layui-btn,.layui-btn-sm,.layuimini-btn-primary").click()//自动点击父级页面表格刷新
                            //TODO 保证速度改为更新修改的表格的单独数据
                            //tr data-index="1"
                            parent.layer.close(index); //再执行关闭
                        },3000);

                    }
                );
            });
        },
        dec: function () {
            layui.form.on('submit(' +'save_form_2'+')', function (data) {
                let option = {url:'dec',data:data.field};
                ea.request.ajax('post',option,
                    function(res){
                        ea.msg.success(res.msg);
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        setTimeout(function (){
                            parent.$(".layui-btn,.layui-btn-sm,.layuimini-btn-primary").click()//自动点击父级页面表格刷新
                            //TODO 保证速度改为更新修改的表格的单独数据
                            //tr data-index="1"
                            parent.layer.close(index); //再执行关闭
                        },3000);

                    }
                );
            });
        },
        // password: function () {
        //     ea.listen();
        // }
    };
    return Controller;
});